package com.softjourn.punchcard.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.actionbarsherlock.app.SherlockActivity;
import com.softjourn.punchcard.R;

public abstract class ExitDialogSherlockActivity extends SherlockActivity {
	
	Dialog dialog;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// Handle the back button
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// Ask the user if they want to quit
			
			dialog = new Dialog(this, R.style.Theme_Punchcard_ExitDialog);
			dialog.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
			dialog.setContentView(R.layout.exit_message);
			dialog.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
			
			Button btnOk = (Button) dialog.findViewById(R.id.btnOk);
			Button btnCancel = (Button) dialog.findViewById(R.id.btnCancel);
			
			btnOk.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					finish();
				}
			});
			
			btnCancel.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					dialog.cancel();
				}
			});
			
			dialog.setCanceledOnTouchOutside(true);
			
			dialog.show();
			
			/*new AlertDialog.Builder(this)
					.setIcon(android.R.drawable.ic_dialog_alert)
					.setTitle(R.string.exit_title)
					.setMessage(R.string.exit_mess)
					.setNegativeButton(R.string.btn_cancel, null)
					.setPositiveButton(R.string.btn_ok,
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// Exit the activity
									finish();
									// moveTaskToBack(true);
								}
							}).show();*/

			// Say that we've consumed the event
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}

	/**
	 * Function to display simple Alert Dialog
	 * 
	 * @param context
	 *            - application context
	 * @param title
	 *            - alert dialog title
	 * @param message
	 *            - alert message
	 * @param status
	 *            - success/failure (used to set icon)
	 * */
	public void showAlertDialog(Context context, String title, String message,
			Boolean status) {

		new AlertDialog.Builder(this)
				.setIcon(
						(status) ? android.R.drawable.ic_dialog_info
								: android.R.drawable.ic_dialog_alert)
				.setTitle(title).setMessage(message)
				.setPositiveButton(android.R.string.ok, null).show();

	}

}
